set more off

use "reassessments", clear
merge m:m swis_code year using "switchers"
keep if _m == 3
drop _m
keep if year > 1986 & year < 2012
keep if mi(Dyr_ub) // drop uncertain years

*** number of years pre and post ***

egen nyearspost = count(year) if D == 1 & !mi(Dyr), by(swis_code)
egen nyearspre = count(year) if D == 0 & !mi(Dyr), by(swis_code)
egen nyearspostm = mean(nyearspost), by(swis_code)
egen nyearsprem = mean(nyearspre), by(swis_code)
replace nyearspost = nyearspostm
replace nyearspre = nyearsprem
drop nyearspostm nyearsprem

*** balance panel ***

local c = 5

keep if (nyearspost >= `c' & nyearspre >= `c')  | mi(nyearspost)

*** pre post indicator and frequency of reassessments pre and post ***

gen prepost =  year - Dyr > 0 & !mi(Dyr)  & year - Dyr <= 10
egen npostreass = sum(reass) if prepost == 1 , by(swis_code)
egen npostreassm = mean(npostreass), by(swis_code)
replace npostreass = npostreassm 
replace npostreass = 0 if mi(Dyr)
drop npostreassm
egen nprereass = sum(reass) if prepost == 0 , by(swis_code)
egen nprereassm = mean(nprereass), by(swis_code)
replace nprereass = nprereassm / nyearspre
replace nprereass = 0 if mi(Dyr)
drop nprereassm

egen ranker = rank(year) if prepost == 1 & reass == 1, by(swis_code)


gen firstyearreass = year if ranker==1
egen firstyearreassm = mean(firstyearreass), by(swis_code)
replace firstyearreass = firstyearreassm - Dyr
drop firstyearreassm

gen secondyearreass = year if ranker == 2
egen secondyearreassm = mean(secondyearreass), by(swis_code)
replace secondyearreass = secondyearreass - Dyr
drop secondyearreassm 

gen thirdyearreass = year if ranker == 3
egen thirdyearreassm = mean(thirdyearreass), by(swis_code)
replace thirdyearreass = thirdyearreass - Dyr
drop thirdyearreassm 

collapse npost first second third, by(swis_code)
gen firstlag = second - first
gen secondlag = third - second

l if npost > 0

replace firstyear = 13 if mi(firstyear) & npost > 0
replace secondyear = 13 if mi(secondyear) & npost > 0

#delimit;

gr tw
	(hist firstyear, disc freq fcol(white))
	,
		plotregion(style(none))
		ylab(, angle(horiz))
		xlab(1(1)10 13 "> 10 years")
		ytitle("Number of towns")
		xtitle("Years")
		name(g1, replace)
		title("Years until first reassessment")
		;
		
#delimit cr



#delimit;

gr tw
	(hist secondyear, disc freq fcol(white))
	,
		plotregion(style(none))
		xlab(2(1)10 13 "> 10 years")		
		xscale(range(1 13))
		ylab(, angle(horiz))
		ytitle("Number of towns")
		xtitle("Years")
		name(g2, replace)
		title("Years until second reassessment")	
		;
		
#delimit cr

gr combine g1 g2, ycommon 

gr export "figure A2.eps", replace
